<?php

namespace App\Lib\PhonePePay;


use PhonePe\common\config\ConfigBuilder;
use PhonePe\common\events\EventPublisher;
use PhonePe\common\models\constants\EventState;
use PhonePe\common\models\events\SdkEvent;
use PhonePe\common\PhonePeClient;
use PhonePe\payments\v1\models\request\builders\InstrumentBuilder;
use PhonePe\payments\v1\models\request\builders\PgPayRequestBuilder;
use PhonePe\payments\v1\PaymentTransactionClient;
use PhonePe\payments\v1\PhonePePaymentClient;

class PhonePePaymentApi
{
    protected $MERCHANTID;
    protected $SALTKEY;
    protected $SALTINDEX;

    protected $ENVIRONMENT;
    protected $SHOULDPUBLISHEVENTS;

    protected $phonePePaymentsClient;

    protected $paymentTransactionClient;

    protected $callbackUrl;

    protected $redirectUrl;




    public function __construct()
    {
        $this->redirectUrl = "https://webhook.in/test/redirect";
        $this->callbackUrl = "https://webhook.in/test/status";
        $this->MERCHANTID = env("PAY_MERCHANTID","");
        $this->SALTKEY = env("PAY_SALTKEY","");
        $this->SALTINDEX = env("PAY_SALTINDEX","1");
        $this->ENVIRONMENT = env("PAY_ENVIRONMENT","UAT");
        $this->SHOULDPUBLISHEVENTS = env("PAY_SHOULDPUBLISHEVENTS",false);
        $this->phonePePaymentsClient = new PhonePePaymentClient($this->MERCHANTID, $this->SALTKEY, $this->SALTINDEX, $this->ENVIRONMENT,$this->SHOULDPUBLISHEVENTS);
    }

    /**
     * 支付
     * @param $merchantTransactionId 支付码
     * @param $mobileNumber  手机号
     * @param $merchantUserId  商户给付款用户分配的id
     * @param $amountInPaise 付款金额
     * @return mixed
     * @throws \PhonePe\common\exceptions\PhonePeException
     */
    public function getPayPage($merchantTransactionId,$mobileNumber,$merchantUserId,$amountInPaise,$callbackUrl = "",$redirectUrl = "")
    {

        $request = PgPayRequestBuilder::builder()
            ->mobileNumber($mobileNumber)
            ->callbackUrl($callbackUrl)
            ->merchantId($this->MERCHANTID)
            ->merchantUserId($merchantUserId)
            ->amount($amountInPaise)
            ->merchantTransactionId($merchantTransactionId)
            ->redirectUrl($redirectUrl)
            ->redirectMode("REDIRECT")
            ->paymentInstrument(InstrumentBuilder::buildPayPageInstrument())
            ->build();
            $response = $this->phonePePaymentsClient->pay($request);
            $PagPageUrl = $response->getInstrumentResponse()->getRedirectInfo()->getUrl();
            return $PagPageUrl;
    }
    /**
     * 查看支付状态
     * @param $merchantTransactionId  支付码
     * @return mixed
     * @throws \Exception
     */
    public function checkStatus($merchantTransactionId)
    {
        $checkStatus = $this->phonePePaymentsClient->statusCheck($merchantTransactionId);
        $state=$checkStatus->getState();
        return $state;
    }

    /**
     * 退款
     * @param $originalTransactionId 原始交易谈好
     * @param $merchantTransactionId  交易单号
     * @return void
     */
    public function refund($originalTransactionId,$merchantTransactionId,$amountInPaise)
    {
        $pgRefundRequest = PgRefundRequestBuilder::builder()
            ->originalTransactionId($originalTransactionId)
            ->merchantId($this->MERCHANTID)
            ->merchantTransactionId($merchantTransactionId)
            ->callbackUrl($this->callbackUrl)
            ->amount($amountInPaise)
            ->build();
        $response = $this->phonePePaymentsClient->refund($pgRefundRequest);
        $responseCode = $response->getResponseCode();
        return $responseCode;
    }

    /**
     * 回调验证
     * @param $xVerify
     * @param $response
     * @return mixed
     */
    public function verifyCallback($xVerify,$response)
    {
        $isValid = $this->phonePePaymentsClient->verifyCallback($response, $xVerify );
        return $isValid;
    }

}
